/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package folders2;

import DlinkedList.DNode;
import SingleLinkedList.*;
import java.io.*;
import DlinkedList.MyDList;
import GoMail.Contacts;
import GoMail.Filter;
import java.io.IOException;
import sorting_searching.SData;

/**
 *
 * @author Administrator
 */
public class UserFolders2 implements UserFolder2Interface
{
    private String userName;
    private ProjectFile myDir;         //account folder
    private ProjectFile inbox;
    private ProjectFile trash;
    private ProjectFile sent;
    private ProjectFile draft;
    private ProjectFile spam;
    private MyDList customFolders;
    private ProjectFile filter;
    private ProjectFile contactsFile;
    /**
     * Contructor to initialize user folder(myDir) or to give reference to user folder
     * @param u  reference to user folder
     */
    public UserFolders2(ProjectFile u)
    {
        myDir = u;

        customFolders = new MyDList();
        //initialize other folders
    }
    /**
     * Contructor to initialize user folders which its references will be set after that
     */
    public UserFolders2()
    {
        myDir = null;
        inbox = null;
        trash = null;
        sent = null;
        draft = null;
        spam = null;
        customFolders = new MyDList();
        filter = null;
        contactsFile = null;
    }
    /**
     * Contructor to initialize user folder and create them
     * @param parent parent folder to user folder (root folder)
     * @param name folder name(user name)
     * @throws java.io.IOException
     */
    public UserFolders2(File parent,String name)throws IOException
    {
        userName = name;
        myDir = new ProjectFile(parent, name);
        myDir.getMyFile().mkdir();
        inbox = new ProjectFile(myDir.getMyFile(), "Inbox");
        inbox.getMyFile().mkdir();
        trash = new ProjectFile(myDir.getMyFile(), "Trash");
        trash.getMyFile().mkdir();
        sent = new ProjectFile(myDir.getMyFile(), "Sent");
        sent.getMyFile().mkdir();
        draft = new ProjectFile(myDir.getMyFile(), "Draft");
        draft.getMyFile().mkdir();
        spam = new ProjectFile(myDir.getMyFile(), "Spam");
        spam.getMyFile().mkdir();
        customFolders = new MyDList();
        filter = new ProjectFile(myDir.getMyFile(),"Filter");
        filter.getMyFile().createNewFile();
        contactsFile = new ProjectFile(myDir.getMyFile(),"contactsFile");
        contactsFile.getMyFile().createNewFile();
    }
    /**
     * This method is used to get contact file
     * @return contact file(or reference to it)
     */
    public ProjectFile getContactsFile()
    {
        return contactsFile;
    }
    /**
     * This method is used to set contact file (give new reference to it)
     * @param contactsFile new contact file
     */
    public void setContactsFile(ProjectFile contactsFile)
    {
        this.contactsFile = contactsFile;
    }
    /**
     * This method is used to get Filter file
     * @return filter file(or reference to it)
     */
    public ProjectFile getFilter()
    {
        return filter;
    }
    /**
     * This method is used to set filter file ( give new reference to it)
     * @param filter new filter file(or new reference to filter file)
     */
    public void setFilter(ProjectFile filter)
    {
        this.filter = filter;
    }
    /**
     * This method is used to get doubly linked list
     * which contains references to all custome folders
     * @return doubly linked list of all custom folders
     */
    public MyDList getCustomFolders()
    {
        return customFolders;
    }
    /**
     * This method is used to set customs folders
     * @param customFolders new list of custom folders
     */
    public void setCustomFolders(MyDList customFolders)
    {
        this.customFolders = customFolders;
    }
    /**
     * This method is used to get reference to draft folder
     * @return reference to draft folder
     */
    public ProjectFile getDraft()
    {
        return draft;
    }
    /**
     * This method is used to set reference to draft folder
     * @param draft  new reference to draft folder
     */
    public void setDraft(ProjectFile draft)
    {
        this.draft = draft;
    }
    /**
     * This method is used to get reference of inbox folder
     * @return  reference of inbox folder
     */
    public ProjectFile getInbox()
    {
        return inbox;
    }
    /**
     * This method is used to set reference of inbox folder
     * @param inbox new reference of inbox folder
     */
    public void setInbox(ProjectFile inbox)
    {
        this.inbox = inbox;
    }
    /**
     * This method is used to get reference of user folder
     * which contains all other folders
     * @return  reference of userfolder folder
     */
    public ProjectFile getMyDir()
    {
        return myDir;
    }
    /**
     * This method is used to set reference of user folder
     * @param myDir new reference of user folder
     */
    public void setMyDir(ProjectFile myDir)
    {
        this.myDir = myDir;
    }
    /**
     * This method is used to get reference of sent folder
     * @return reference of user folder
     */
    public ProjectFile getSent()
    {
        return sent;
    }
    /**
     * This method is used to set reference of sent folder
     * @param sent new reference of sent folder
     */
    public void setSent(ProjectFile sent)
    {
        this.sent = sent;
    }
    /**
     * This method is used to get reference of spam folder
     * @param reference of spam folder
     */
    public ProjectFile getSpam()
    {
        return spam;
    }
    /**
     * This method is used to set new reference of spam folder
     * @param spam new reference of spam folder
     */
    public void setSpam(ProjectFile spam)
    {
        this.spam = spam;
    }
    /**
     * This method is used to get reference of trash folder
     * @return reference of spam folder
     */
    public ProjectFile getTrash()
    {
        return trash;
    }
    /**
     * This method is used to set reference of trash folder
     * @param trash new reference of trash folder
     */
    public void setTrash(ProjectFile trash)
    {
        this.trash = trash;
    }
    /**
     * This method is used to get userName
     * @return userName
     */
    public String getUserName()
    {
        return userName;
    }
    /**
     * This method is used to set userName
     * @param userName new userName
     */
    public void setUserName(String userName)
    {
        this.userName = userName;
    }
    /**
     * This method is used to create new custom folders in user directory beside user folder
     * @param folderName name of new folder
     */
    public void createCustomFolder(String folderName)
    {
        ProjectFile u = new ProjectFile(myDir.getMyFile(), folderName);
        u.getMyFile().mkdir();
        customFolders.add(u);       //////add ProjectFile
    }
    /**
     * This method is used to delete custom folder
     * @param folderName folder name
     */
    public void deleteCustomFolder(String folderName)
    {
        SData list = new SData();
        list.copyList(customFolders, folderName);
        list.setCompareKey(2);          //compare between nodes
        list.sortList();
        DNode u = (DNode)list.binSearch();
        DNode v = u.getNext();
        DNode w = u.getPrev();
        v.setNext(w);
        w.setPrev(v);
        u.setNext(null);
        u.setPrev(null);
        u = null;
    }
}
